extends Node
# 全局只能有一个Suns类
var sunbox = null

# 初始化logger
class Log:
	enum available_lvl {
		DEBUG=0,
		INFO=1,
		WARNING=2,
		ERROR=3
	}
	var lvl = available_lvl.DEBUG
	func _init(level: available_lvl) -> void:
		self.lvl = level
	func debug(s: String):
		if available_lvl.DEBUG >= self.lvl:
			print("[DEBUG] %s"%s)
	func info(s: String):
		if available_lvl.INFO >= self.lvl:
			print("[INFO] %s"%s)
	func warning(s: String):
		if available_lvl.WARNING >= self.lvl:
			push_warning("[WARNING] %s"%s)
	func error(s: String):
		if available_lvl.ERROR >= self.lvl:
			push_error("[ERROR] %s"%s)

var log: Log = Log.new(Log.available_lvl.DEBUG)
# 移动和旋转相机
var rotate_btn: bool = false # 负责旋转画面的按钮 抬起为f 按下为t
var sensitive: float = 0.001 # 灵敏度
# 菜单
var menu_box: Control = null
var menu_box_res: Resource = preload("res://scene/profile_win.tscn")

class menu_planet:
	var mass: float = 1.
	var diam: float = 1.
	var loc: Vector3 = Vector3.ZERO
	var acc: Vector3 = Vector3.ZERO
	var vel: Vector3 = Vector3.ZERO
	
	func deep_copy() -> menu_planet:
		# 深拷贝
		var cpy: menu_planet = menu_planet.new()
		cpy.mass = self.mass
		cpy.diam = self.diam
		cpy.loc = self.loc
		cpy.acc = self.acc
		cpy.vel = self.vel
		return cpy
		
class menu_global:
	var const_g: float = 6.674e-11
	var fps: int = 60
	var sensitive: float = 0.001
	var tail: float = 0.
	
	func deep_copy() -> menu_global:
		# 深拷贝
		var cpy: menu_global = menu_global.new()
		cpy.const_g = self.const_g
		cpy.fps = self.fps
		cpy.sensitive = self.sensitive
		cpy.tail = self.tail
		return cpy

var tmp_menu: Dictionary = {}

# 启动流程
# 先尝试读取用户配置 如果没有 尝试读取默认配置
# 根据配置生成menu_planet和menu_global类
# 根据配置从world初始化Suns和Sun
# 如果用户有新配置 则覆盖用户配置

var global_config: menu_global = menu_global.new()
var sun_1_config: menu_planet = menu_planet.new()
var sun_2_config: menu_planet = menu_planet.new()
var sun_3_config: menu_planet = menu_planet.new()

# 用户界面更新配置临时储存
var tmp_global_config: menu_global = null
var tmp_sun_1_config: menu_planet = null
var tmp_sun_2_config: menu_planet = null
var tmp_sun_3_config: menu_planet = null

func _get_profile(pth: String) -> bool:
	var f: FileAccess = FileAccess.open(pth, FileAccess.READ)
	if not f:
		return false
	var profile: Dictionary = JSON.parse_string(f.get_as_text())
	f.close()
	# 获取全局设置
	var tmp = profile["global"]
	global_config.const_g = tmp["const_g"]
	global_config.fps = tmp["fps"]
	global_config.sensitive = tmp["sensitive"]
	global_config.tail = tmp["tail"]
	# 获取suns设置
	tmp = profile["suns"]
	sun_1_config.mass = tmp[0]["mass"]
	sun_2_config.mass = tmp[1]["mass"]
	sun_3_config.mass = tmp[2]["mass"]
	
	sun_1_config.diam = tmp[0]["diam"]
	sun_2_config.diam = tmp[1]["diam"]
	sun_3_config.diam = tmp[2]["diam"]
	
	sun_1_config.loc = Vector3(tmp[0]["loc"][0], tmp[0]["loc"][1], tmp[0]["loc"][2])
	sun_2_config.loc = Vector3(tmp[1]["loc"][0], tmp[1]["loc"][1], tmp[1]["loc"][2])
	sun_3_config.loc = Vector3(tmp[2]["loc"][0], tmp[2]["loc"][1], tmp[2]["loc"][2])
	
	sun_1_config.vel = Vector3(tmp[0]["vel"][0], tmp[0]["vel"][1], tmp[0]["vel"][2]) 
	sun_2_config.vel = Vector3(tmp[1]["vel"][0], tmp[1]["vel"][1], tmp[1]["vel"][2]) 
	sun_3_config.vel = Vector3(tmp[2]["vel"][0], tmp[2]["vel"][1], tmp[2]["vel"][2]) 
	
	sun_1_config.acc = Vector3(tmp[0]["acc"][0], tmp[0]["acc"][1], tmp[0]["acc"][2])
	sun_2_config.acc = Vector3(tmp[1]["acc"][0], tmp[1]["acc"][1], tmp[1]["acc"][2])
	sun_3_config.acc = Vector3(tmp[2]["acc"][0], tmp[2]["acc"][1], tmp[2]["acc"][2])
	log.debug("配置文件内容:" + str(profile))
	return true
func _get_default_profile():
	if not _get_profile("res://default_config.json"):
		log.error("无法获取默认配置")
func _get_user_profile():
	if not _get_profile("user://user_config.json"):
		log.info("无法获取用户配置 正在读取默认配置并生成用户配置")
		# 将默认配置复制到用户配置
		var f: FileAccess = FileAccess.open("user://user_config.json", FileAccess.WRITE)
		var f_default: FileAccess = FileAccess.open("res://default_config.json", FileAccess.READ)
		f.store_string(f_default.get_as_text())
		f.close()
		f_default.close()
		# 获取默认配置
		_get_default_profile()
	else:
		log.info("已成功获取用户配置")

func _set_user_profile(data):
	# 将默认配置复制到用户配置
	var f: FileAccess = FileAccess.open("user://user_config.json", FileAccess.WRITE)
	if not f:
		log.error("用户配置文件为只读 保存失败")
		return
	var j = JSON.stringify(data)
	f.store_string(j)
	f.close()

func _vec3_to_a(x: Vector3) -> Array:
	return [x.x, x.y, x.z]
